package io.renren.data;

import io.renren.RenrenApplication;
import io.renren.modules.avanotes.dao.ReferenceMaterialDao;
import io.renren.modules.avanotes.entity.ReferenceMaterialEntity;
import io.renren.modules.avanotes.service.ReferenceMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.core.util.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: xiaomi
 * @date: 2022/2/21
 * @description: 参考资料导入
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RenrenApplication.class)
@Slf4j
public class ReferenceImport {

    String FILE_EXTENSIONS = ".pdf.doc.md";
    @Autowired
    ReferenceMaterialDao rmDao;

    @Autowired
    ReferenceMaterialService rfService;

    /**
     * 导入参考资料
     * md pdf doc 三种文件录入
     */
    @Test
    public void importReferenceMaterials() {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("D:\\文档\\面试\\JavaGuide-main", 1);
        map.put("D:\\文档\\面试\\JavaHome-master", 2);
        map.put("D:\\文档\\面试\\Java-Interview-master-xbox-1994", 3);
        map.put("D:\\文档\\面试\\Java-Interview-master", 4);
        map.put("D:\\文档\\面试\\Java-Interview-Advanced-master", 5);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            importMaterials(entry);
        }
    }

    /**
     * 导入素材
     *
     * @param entry
     */
    private void importMaterials(Map.Entry<String, Integer> entry) {
        List<ReferenceMaterialEntity> ll = new ArrayList<ReferenceMaterialEntity>();

        File dir = new File(entry.getKey());
        addFile2List(dir, ll, entry.getValue());
        rfService.saveBatch(ll);
    }

    private void addFile2List(File dir, List<ReferenceMaterialEntity> ll, int bankId) {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                addFile2List(file, ll, bankId);
            } else {
                //如果不是目录 - 判断文件名
                String fileExtension = FileUtils.getFileExtension(file);
                if (StringUtils.isEmpty(fileExtension)) {
                    continue;
                }
                fileExtension = fileExtension.toLowerCase();
                int index = FILE_EXTENSIONS.indexOf(fileExtension);
                if (index >= 0) {
                    String fileName = file.getName();
                    String path = file.getAbsolutePath();
                    //如果符合
                    ll.add(new ReferenceMaterialEntity(bankId, fileName, 1, getResourceType(index), path));
                }
            }
        }
    }

    private int getResourceType(int index) {
        if (index == 1) {
            return 2;
        } else if (index == 5) {
            return 3;
        } else if (index == 9) {
            return 1;
        }
        return 0;
    }
}
